package com.zhkj.crawl.utils;

import com.google.common.base.Joiner;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;

import java.util.Arrays;
import java.util.List;

public class ChromeMethodExt {
    public static ExpectedCondition<Boolean> or(ExpectedCondition<?>[] conditions) {
        return or(Arrays.asList(conditions));
    }

    public static ExpectedCondition<Boolean> or(List<ExpectedCondition<?>> conditions) {
        return new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                RuntimeException lastException = null;
                int var4 = conditions.size();

                for(int var5 = 0; var5 < var4; ++var5) {
                    ExpectedCondition condition = conditions.get(var5);

                    try {
                        Object result = condition.apply(driver);
                        if (result != null) {
                            if (!(result instanceof Boolean)) {
                                return true;
                            }

                            if (Boolean.TRUE.equals(result)) {
                                return true;
                            }
                        }
                    } catch (RuntimeException var8) {
                        lastException = var8;
                    }
                }

                if (lastException != null) {
                    throw lastException;
                } else {
                    return false;
                }
            }

            public String toString() {
                StringBuilder message = new StringBuilder("at least one condition to be valid: ");
                Joiner.on(" || ").appendTo(message, conditions);
                return message.toString();
            }
        };
    }
}
